
import { _decorator, Component, Node, Label, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
 
@ccclass('UICountdownController')
export class UICountdownController extends Component {

    private _LblCountDown: Label = null;

    private _FuncCompleted: Function = null;
    private _nCountdown: number = 0;

    onLoad () {
        this._LblCountDown = this.node.getChildByName("BgDialog").getChildByName("LblCountdown").getComponent(Label);
    }

    start () {

    }

    setCountDown(nCountdown: number, funcCompleted: Function) {
        this._nCountdown = nCountdown;
        this._FuncCompleted = funcCompleted;

        this.schedule(this._callback, 1, nCountdown);        
    }


    private _callback () {
        this._LblCountDown.string = ""+(--this._nCountdown);
        if (this._nCountdown <= 1) {
            this.unschedule(this._callback);

            var self = this
            this.scheduleOnce(function() {
                self._LblCountDown.string = "Go"
                tween(self._LblCountDown.node).to(0.2, { scale: new Vec3(1.5, 1.5, 1) }).delay(0.2).to(0.3, { scale: new Vec3(1, 1, 1) }).delay(0.3).call(function() {
                    if (self._FuncCompleted) {
                        self._FuncCompleted();
                    }
                }).start();
            }, 1);
        }
    }
    
}
